package org.yray.sip.creator;

import org.yray.sip.base.MsgEle;
import org.yray.sip.server.SipSession;
import org.yray.sip.utils.IdUtils;

public class ProgressCreator extends MessageCreator{

    /**
     * SIP/2.0 183 Session Progress
     * Via: SIP/2.0/UDP 192.168.44.84:56823;rport=56823;branch=z9hG4bKPj79ae6c6119494dd8b9cfc15dee75e2a7
     * From: <sip:78006@172.168.200.78>;tag=e5a173b440c04ee58b25f1eee3f2c6e9
     * To: <sip:LE0155513430565773@172.168.200.78>;tag=3re7K4Z67aZrF
     * Call-ID: 0010d51b92004a1e9e6a9fb9e0d9646f
     * CSeq: 20763 INVITE
     * Contact: <sip:LE0155513430565773@172.168.200.78:5060;transport=udp>
     * User-Agent: OE+ PBX
     * Accept: application/sdp
     * Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY, PUBLISH, SUBSCRIBE
     * Supported: path, replaces
     * Allow-Events: talk, hold, conference, presence, as-feature-event, dialog, line-seize, call-info, sla, include-session-description, presence.winfo, message-summary, refer
     * Content-Type: application/sdp
     * Content-Disposition: session
     * Content-Length: 260
     * Remote-Party-ID: "LE0155513430565773" <sip:LE0155513430565773@172.168.200.78>;party=calling;privacy=off;screen=no
     *
     * v=0
     * o=FreeSWITCH 1665530760 1665530761 IN IP4 172.168.200.78
     * s=FreeSWITCH
     * c=IN IP4 172.168.200.78
     * t=0 0
     * m=audio 29834 RTP/AVP 8 101
     * a=rtpmap:8 PCMA/8000
     * a=rtpmap:101 telephone-event/8000
     * a=fmtp:101 0-16
     * a=ptime:20
     * a=rtcp:29835 IN IP4 172.168.200.78
     * @param session
     * @return
     */
    @Override
    public String createMsg(SipSession session) {
        StringBuilder sb = new StringBuilder();
        sb.append("SIP/2.0 183 Session Progress").append(MsgEle.LINE_SEPARATOR);
        sb.append("Via: SIP/2.0/UDP ").append(session.getSipIpPort()).append(";rport;branch=").append(session.getBranch()).append(MsgEle.LINE_SEPARATOR);
        sb.append("From: "+session.getSipExtIpName()+";tag="+session.getTag()).append(MsgEle.LINE_SEPARATOR);
        sb.append("To: "+session.getToExt()).append(MsgEle.LINE_SEPARATOR);
        sb.append("Call-ID: "+session.getCallId()).append(MsgEle.LINE_SEPARATOR);
        sb.append("CSeq: "+session.getCseq()+" INVITE").append(MsgEle.LINE_SEPARATOR);
        sb.append("Contact: "+session.getContact()).append(MsgEle.LINE_SEPARATOR);
        sb.append("User-Agent: ROBOT NO1").append(MsgEle.LINE_SEPARATOR);
        sb.append("Accept: application/sdp").append(MsgEle.LINE_SEPARATOR);
        sb.append("Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS").append(MsgEle.LINE_SEPARATOR);
        sb.append("Supported: path, replaces").append(MsgEle.LINE_SEPARATOR);
        sb.append("Allow-Events: talk, hold, conference, presence, as-feature-event, dialog, line-seize, call-info, sla, include-session-description, presence.winfo, message-summary, refer").append(MsgEle.LINE_SEPARATOR);
        sb.append("Content-Type: application/sdp").append(MsgEle.LINE_SEPARATOR);
        sb.append("Content-Disposition: session").append(MsgEle.LINE_SEPARATOR);

        int rtcpPort=session.getLocalPort()+11;
        int rtpPort=session.getLocalPort()+10;
        StringBuilder sdp=new StringBuilder();
        sdp.append("v=0").append(MsgEle.LINE_SEPARATOR);
        sdp.append("o=ROBOT "+ IdUtils.getId()+" 0 IN IP4 "+session.getLocalIp()).append(MsgEle.LINE_SEPARATOR);
        sdp.append("s=ROBOT").append(MsgEle.LINE_SEPARATOR);
        sdp.append("b=AS:84").append(MsgEle.LINE_SEPARATOR);
        sdp.append("t=0 0").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=X-nat:0").append(MsgEle.LINE_SEPARATOR);
        sdp.append("m=audio "+rtpPort+" RTP/AVP 8 101").append(MsgEle.LINE_SEPARATOR);
        sdp.append("c=IN IP4 "+session.getLocalIp()).append(MsgEle.LINE_SEPARATOR);
        sdp.append("b=TIAS:64000").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=rtcp:"+rtcpPort+" IN IP4 "+session.getLocalIp()).append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=sendrecv").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=rtpmap:8 PCMA/8000").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=rtpmap:101 telephone-event/8000").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=fmtp:101 0-16").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=ssrc:"+session.getSsrc()+" cname:"+session.getCname()).append(MsgEle.LINE_SEPARATOR);
        sb.append("Content-Length:   ").append(sdp.length()).append(MsgEle.LINE_SEPARATOR);
        sb.append(MsgEle.LINE_SEPARATOR);
        sb.append(sdp.toString());
        sb.append(MsgEle.LINE_SEPARATOR);
        return sb.toString();
    }
}
